Dart allows to declare variables and fields without specifying their type, even when it is not initialized inline. In that case, the type is
inferred from the first initialization.
However, the first initialization can be far from the declaration, making the type of the variable not obvious, just by looking at its
declaration.
void foo() {
var path; // String or Path?
// ...
path = defaultPath;
// ...
}
Moreover, the first initialization expression can be complex, making it hard to understand what the type of the variable is even when the
initialization is close enough to the declaration.
void foo() {
var path; // String or Path?
// ...
path = buildPath();
// ...
}
In addition, there are scenarios where type inference is not advisable, like when the desired type is not the narrowest matching
initialization:
var x; // x is inferred as an int, becase of its first initialization
// ...
x = 3;
// ...
x = 4.0; // Error: A value of type 'double' can't be assigned to a variable of type 'int'
To avoid confusion and make the intent clear, it’s recommended to always specify the type of initialized variables and fields explicitly.